<?php
class Mysql_DB {
	var $db;   //db conncion
	var $result;  //result set
		
	function Mysql_DB ($db_host, $db_user, $db_pass, $db_name) {
		$this->connect_db($db_host, $db_user, $db_pass);
		$this->select_db($db_name);

	}
	
	function connect_db($db_host, $db_user, $db_pass) {
		$this->db = @mysql_connect($db_host, $db_user, $db_pass);
		if(is_resource($this->db) == false) {
			$this->trace_error($db_host."에 접속할수 없습니다.");
		}
	
	}
	
	function select_db($db_name) {
		@mysql_select_db($db_name, $this->db) or $this->trace_error($db_name."을 선택할수 없습니다.");
	}
	
	
	function execute_query($query) {
	
		if(is_resource($this->db) == false) {
			$this->trace_error("execute_query error. db 접속이 유효하지 않습니다.");
			return;
		}
		$this->result = @mysql_query($query, $this->db) or $this->trace_error("쿼리문에 문제가 있습니다.");
	}
	
	
	function execute_update($query) {
		$result = mysql_query($query, $this->db); //
		if($result) return mysql_affected_rows();
		return -1;
	}
	
	function insert_id($query) {
		$result = mysql_query($query, $this->db); //
		if($result) return mysql_insert_id();
		return -1;
	}
	
	function get_result() {
		return $this->result;
	}
	
	function execute_simple_query($query) {
		$results = mysql_query($query, $this->db);
		if(!is_resource($results)) return -1;
		return mysql_result($results, 0, 0);
			
	}
	
	function get_num_rows() {
		if(is_resource($this->result)) return mysql_num_rows($this->result);
		return -1;
	}
	
	function get_array() {
		return mysql_fetch_array($this->result);
	}
	
	function next_fetch() {
		return mysql_fetch_assoc($this->result);
	}
	
	function next_row() {
		return mysql_fetch_assoc($this->result);
	}
	
	function data_seek($i) {
		return mysql_data_seek($this->result,$i);
	}
	
	function trace_error($msg) {
		echo "</UL></DL></OL>\n";
		echo "</TABLE></SCRIPT>\n";
		$text  = "<FONT COLOR=\"#ff0000\"><P>Error: $msg : </p>";
		$text .= mysql_error();
		$text .= "</FONT>\n";
		die($text);
	}
	
	function free_result() {
		mysql_free_result($this->result);
	}
	
	function destroy() {
		if(is_resource($this->result)) mysql_free_result($this->result);
		mysql_close($this->db);
	}
}
?>
	
	
	